#pragma once

#include<stdlib.h>
#include<stdio.h>
#include<array>

using namespace std;

class BinSort
{
public:
	BinSort(int data[], int size)
	{
		datalist = data;
		datasize = size;
		int maxVal = datalist[0];
		for (int index = 0; index < size; index++)
		{
			if (datalist[index] > maxVal)
				maxVal = datalist[index];
		}//end loop

		binlist = new int[maxVal];
		binsize = maxVal;

		for (int index = 0; index < binsize; index++)
			binlist[index] = 0;

		performBinSort();
	}//end cons

	void performBinSort()
	{
		for (int index = 0; index < datasize; index++)
			binlist[datalist[index] - 1] ++;

		int dataIterator = 0;
		for (int index = 0; index < binsize; index++)
		{
			if (binlist[index] != 0)
			{
				int datanum = binlist[index];
				for (int i = 0; i < datanum; i++)
				{
					datalist[dataIterator] = index + 1;

					////Testing code:
					//printf("Data: %d\n", index);
					//printf("Appeared: %d\n", datanum = binlist[index]);

					dataIterator++;
				}//store data into original data array.
			}//end if: If there are data representing certain original elements.
		}//end for

		printf("\n");
	}//end method: Perform bin sort

	int * getSortedArray()
	{
		return datalist;
	}//Get sorted array

	~BinSort()
	{
		printf("Bin sort finishes!\n");
		system("pause");
	}//end distructor

private:
	int datasize;
	int binsize;
	int * datalist;
	int * binlist;
};//end class